"""
工具函数和辅助方法
"""
import uuid
import datetime
import config

def generate_message_id(prefix="msg"):
    """生成唯一消息ID"""
    return f"{prefix}_{str(uuid.uuid4())[:8]}"

def get_timestamp():
    """获取当前ISO格式时间戳"""
    return datetime.datetime.now().isoformat()

def add_to_chat_history(message, is_user=True):
    """添加消息到聊天历史"""
    message_id = generate_message_id("user" if is_user else "assistant")
    timestamp = get_timestamp()
    
    history_item = {
        "message_id": message_id,
        "type": "user" if is_user else "assistant",
        "content": message["content"] if not is_user else message,
        "timestamp": timestamp
    }
    
    # 如果是AI回复，添加SQL和结果
    if not is_user and "sql_generated" in message:
        history_item["sql_generated"] = message["sql_generated"]
        history_item["results"] = message["results"]
    
    config.CHAT_HISTORY.append(history_item)
    return message_id
